﻿
Chenxin.UI.CommunityBuildingMgr=Chenxin.extend(Chenxin.UI.PageCommon,{
	actionUrl:'service/CommunityBuildingMgr.ashx',
	pageSize:4,
	createGrid:function(){		
		return new Chenxin.UI.Pagination({
			actionUrl:this.actionUrl+'?op=getlist',
			id:'communitybuilding',
			renderTo: 'communitybuilding_table',
			pageSize:this.pageSize,
			autoLoad:this.autoLoadGrid,
		});
	},
	getStatus:function(index){
		if(this.currentId){
			return $('#'+this.currentId).find('td').eq(index||4).text();
		}
		return "";
	},
	bulkAdd:function(){
		this.createBulkForm();
		this.bulkForm.show();
		this.bulkForm.fitData({BuildingNameType:'栋',MinNumber:'1',MaxNumber:'1',LengthNumber:'1',MinChar:'A',MaxChar:'A',BuildingName:'(*)',step:'1'});
	},
	createBulkForm:function(){
		if(!this.bulkForm){
			this.bulkForm=new Chenxin.UI.CommunityBuildingInfoBulkForm({
				scope:this,
				saveSucc:function(){
					if($('#'+this.grid.renderTo).length){
						this.grid.reload('',0);
					}				
				}
			});
		}		
	},
	/**
	*@description Ajax交互，对选中记录进行删除
	*/
	logicDel:function(autoConfirm,ch,confirmMsg,op){//删除操作
		if(!this.currentId){
			alert('请选择一条记录！');
			return;			
		}
	
		var _this=this;
		var d=true;
		if(autoConfirm){
			d=confirm(confirmMsg||'确定要删除该条记录吗？');
		}
        if(d){
			Chenxin.ajax({				
				type: "get",
				dataType: "json",
				url: this.actionUrl,
				data: {op:op||'logicdel',modelid:this.currentId,checkhouse:ch||'true'},
				onSuccess: function(data,msg) {
					if(data.hasHouse){
						if(confirm('该楼栋已经添加过房源，确定执行逻辑删除吗？')){
							_this.logicDel(false,'false');
						}
					}else{					
						alert(msg);
						if(_this.grid){
							_this.grid.reload();
						}
					}
				}
			});
		}			
	},
	initEvent:function(){		
		var _this=this;
		$("#btn_add_building").live("click", function(){
			_this.form.fitData({BuildingNameType:'栋'});
			_this.add();
			return false;
		});
		$("#btn_bulkadd_building").live("click", function(){
			_this.bulkAdd();
			return false;
		});
		$('#goon_addbuilding').click(function(){			
			$('#addbuild_msg').jqmHide();
			_this.form.reset();
			_this.add();
			return false;
		});
		$('#edit_building').click(function(){		
			$('#addbuild_msg').jqmHide();
			_this.edit();
			return false;
		});
		$('.a-communitybuilding-edit').live('click',function(){
			_this.currentId=this.name;
			_this.edit();
			return false;
		});
		
		$('.a-communitybuilding-del').live('click',function(){
			_this.currentId=this.name;
			_this.del();
			return false;
		});
		
		
		$('.a-communitybuilding-logicdel').live('click',function(){
			_this.currentId=this.name;
			_this.logicDel(true);
			return false;
		});
		
		$('.a-communitybuilding-lock').live('click',function(){
			_this.currentId=this.name;
			var _status=_this.getStatus();
			if(_status=="已冻结"){
				alert('状态为“冻结”的楼栋，不能执行冻结操作！');
				return false;
			}else if(_status=="已删除"){			
				alert('状态为“已删除”的楼栋，不能执行冻结操作！');	
				return false;
			}
            _this.logicDel(true,'false','确定冻结该楼栋吗？该楼盘被冻结后将不能添加房源！','lock');
			return false;
		});
		
		$('.a-communitybuilding-unlock').live('click',function(){
			_this.currentId=this.name;
			var _status=_this.getStatus();
			if(_status=="正常"){				
				alert('状态为“正常”的楼栋，不能执行激活操作！');
				return false;
			}else if(_status=="已删除"){				
				alert('状态为“已删除”的楼栋，不能执行激活操作！');
				return false;
			}
            _this.logicDel(true,'false','确定激活该楼栋吗？','unlock');
			return false;
		});
		
		
		$('#a-goon-add').live('click',function(){			
			//$('#addbuild_msg').jqmHide();
			Chenxin.Msg.alert.hide();
			_this.form.reset();
			_this.add();
			return false;
		});
		$('#a-edit-building').live('click',function(){		
			//$('#addbuild_msg').jqmHide();
			Chenxin.Msg.alert.hide();
			_this.edit();
			return false;
		});
		
	},
	createForm:function(){
		return new Chenxin.UI.CommunityBuildingInfoForm({
			scope:this,
			afterGetFormJson:function(json){
				json.BuildingName+=(json.BuildingNameType||'');
				json.CommunityId=this.communityId;
				json.RoomRuleRemark=$("#"+this.form.id+" :radio[name='RoomRule'][checked='true']").attr('title');	
				if(this.form.op=='edit'){
					this.form.additionalParam.oldBulidingName=this.currentModel.BuildingName;
				}else{
					if(this.form.additionalParam.oldBulidingName){
						delete this.form.additionalParam.oldBulidingName;
					}
				}
				
				delete json.BuildingNameType;
				return json;
			},
			beforeFitData:function(model){
				if(!model.BuildingName){
					return;
				}
				if(model.BuildingName.indexOf('座')==model.BuildingName.length-1||model.BuildingName.indexOf('号')==model.BuildingName.length-1||model.BuildingName.indexOf('栋')==model.BuildingName.length-1||model.BuildingName.indexOf('幢')==model.BuildingName.length-1){		
					var name=model.BuildingName;
					model.BuildingName=name.substring(0,name.length-1);
					model.BuildingNameType=name.substring(name.length-1,name.length)
				}else{
					model.BuildingNameType='';
				}
			},
			saveSucc:function(data,msg){
				if($('#'+this.grid.renderTo).length){
					//this.reloadGrid();//grid.reload();
					if(this.form.op=='edit'){
						this.grid.reload(this.grid.data,true,this.grid.pageIndex);
					}else{
						this.grid.reload('',0);
					}
				}				
				this.currentId=data.BuildingId;
				
				Chenxin.Msg.alert.show('<a id="a-goon-add" title="继续添加楼栋" href="#">继续添加楼栋</a> <br/><br/><a id="a-edit-building" title="编辑该楼栋" href="#">编辑该楼栋</a>','下一步',Chenxin.Msg.icon.next);
				//$('#addbuild_msg').jqmShow();		
			}
		});
	},
	initComponent:function(){
		Chenxin.UI.CommunityBuildingMgr.superclass.initComponent.call(this);
	}
});

Chenxin.UI.CommunityBuildingInfoBulkForm=Chenxin.extend(Chenxin.UI.FormCommon,{
	actionUrl:'service/CommunityBuildingMgr.ashx',
	title:'批量添加',
	id:'communityBuildingBulkWin',
	renderTo:'jqm_communityBuildingBulk',
	initFormValidator:function(){
		var _this=this;
		
        //楼栋层数验证
        $("#communitybuildingbulk_Floor").formValidator({
			validatorgroup:this.id,
        onshow: "请输入楼栋层数!", onfocus: "请输入楼栋层数!", oncorrect: " "
        }).inputValidator({ //非空验证
        min: 1, empty: { leftempty: false, rightempty: false, emptyerror: "楼栋层数不能为空!" }, onerror: "楼栋层数不能为空!"
        }).functionValidator({ //数字验证
            fun:function(val,elem) { return val.isNum();}, onerror: "请输入数字"
        });
        
        //单元数验证
        $("#communitybuildingbulk_UnitNum").formValidator({		
			validatorgroup:this.id,
        onshow: "请输入单元数!", onfocus: "请输入单元数!", oncorrect: " "
        }).functionValidator({ //数字验证
            fun:function(val,elem) { return val.isNum();}, onerror: "请输入数字"
        });
	
	},
	afterGetFormJson:function(model){
		this.bulkForm.additionalParam={};
		if(model.setting=='number'){		
			this.bulkForm.additionalParam.MinNumber=model.MinNumber;
			this.bulkForm.additionalParam.MaxNumber=model.MaxNumber;
			this.bulkForm.additionalParam.LengthNumber=model.LengthNumber;
		}else{
			this.bulkForm.additionalParam.MinChar=model.MinChar;
			this.bulkForm.additionalParam.MaxChar=model.MaxChar;
			this.bulkForm.additionalParam.LengthNumber=model.LengthNumber;
		}
		if(model.BuildingNameType){
			this.bulkForm.additionalParam.BuildingNameType=model.BuildingNameType;		
		}
		this.bulkForm.additionalParam.setting=model.setting;
		if(model.step){
			this.bulkForm.additionalParam.step=model.step;
			delete model.step;
		}
		model.CommunityId=this.communityId;
		model.RoomRuleRemark=$("#"+this.bulkForm.id+" :radio[name='RoomRule'][checked='true']").attr('title');	
		
		
		delete model.setting;
		delete model.MinChar;
		delete model.MaxChar;
		delete model.MinNumber;
		delete model.MaxNumber;
		delete model.LengthNumber;
		delete model.BuildingNameType;
		this.bulkForm.op='bulkadd';
	},
	customerFormVal:function(json){
		try{
			if(json.BuildingName.indexOf('*')==-1){
				alert('楼栋名称必须包含"*"');
				return false;
			}else if(json.BuildingName.lastIndexOf('*')!=json.BuildingName.indexOf('*')){				
				alert('楼栋名称只能包含一个"*"');
				return false;
			}
			var model=this.additionalParam;
			if(model.setting=='number'){
				var num=/^[0-9]{1,5}$/;
				if(!num.test(model.MinNumber)||!num.test(model.MaxNumber)||!num.test(model.LengthNumber)){
					alert('配置项必须为数字,且位数不允许大于5位！');
					return false;
				}
				if(parseInt(model.LengthNumber)>5||parseInt(model.LengthNumber)<1){
					alert('长度不能大于5和小于1!');
					return false;
				}
			}else{
				var ch=/^[A-Z]{1}$/;
				if(!ch.test(model.MinChar)||!ch.test(model.MaxChar)){
					alert('配置项必须为大写字母A-Z！');
					return false;
				}
			}
			return true;
		}catch(e){
			alert('配置项设置错误！');
			return false;
		}
	},
	createForm:function(){
		return Chenxin.UI.FormCommon.communityBuildingInfoBulkForm('communitybuildingbulk');
	},
	initEvent:function(){		
		var _this=this;
		$('#communitybuildingbulk_Save').live('click',function(){
			_this.save();
			return false;
		});
		$('#communitybuildingbulk_Cancle').live('click',function(){
			_this.hide();
			return false;
		});		
	},
	createComboList:function(){	
		var nameType=new Chenxin.UI.Combo({
				containerId: '#communitybuildingbulk_BuildingNameType',
				name:'BuildingNameType',
				addPleaseSelected:false,
				items:comboCommon.buildingNameType,
				required:true			
			});
		var unitType=new Chenxin.UI.Combo({
				containerId: '#communitybuildingbulk_UnitType',
				name:'UnitType',
				items:comboCommon.buildingUnitType
			});
		return [nameType,unitType];
	},
	initComponent:function(){
		Chenxin.UI.CommunityBuildingInfoBulkForm.superclass.initComponent.call(this);
	}
});

Chenxin.UI.CommunityBuildingInfoForm=Chenxin.extend(Chenxin.UI.FormCommon,{
	actionUrl:'service/CommunityBuildingMgr.ashx',
	id:'communityBuildingWin',
	renderTo:'jqm_communityBuilding',
	defBBtn:true,
	show:function(){
		if(this.op=='add'){
			this.setTitle('添加楼栋');
		}else if(this.op=='edit'){
			this.setTitle('编辑楼栋');
		}
		return Chenxin.UI.CommunityBuildingInfoForm.superclass.show.call(this);
	},
	initFormValidator:function(){
		var _this=this;
		
        //楼栋名称验证
        $("#txtBuildingName").formValidator({		
			validatorgroup:this.id,
            onshow: "请输入楼栋名称!", onfocus: "请输入楼栋名称!", oncorrect: " "
        }).inputValidator({ //非空验证
            min: 1, empty: { leftempty: false, rightempty: false, emptyerror: "楼栋名称不能为空!" }, onerror: "楼栋名称不能为空!"
        }).functionValidator({ //数字验证
	        fun: function(val, elem) { 
				if(_this.op=='edit'&&_this.scope.currentModel.BuildingName==val){
					return true;
				}
				var _check=false;
				Chenxin.ajax({
					url:_this.actionUrl,
					data:{op:'validate',communityid:_this.scope.communityId,txtBuildingName:val,clientid:elem.id},
					onSuccess:function(data,msg){
						if(msg){
							_check=false;
						
						}else{
							_check=true;						
						}
					},
					async:false,
					onFail:function(){
						_check=false;
					}
				});
				return _check;
	        }, 
			onerror: "楼栋名称已存在"
	    });

        //楼栋层数验证
        $("#txtFloor").formValidator({
			validatorgroup:this.id,
        onshow: "请输入楼栋层数!", onfocus: "请输入楼栋层数!", oncorrect: " "
        }).inputValidator({ //非空验证
        min: 1, empty: { leftempty: false, rightempty: false, emptyerror: "楼栋层数不能为空!" }, onerror: "楼栋层数不能为空!"
        }).functionValidator({ //数字验证
            fun:function(val,elem) { return val.isNum();}, onerror: "请输入数字"
        });
        
        //单元数验证
        $("#txtUnitNum").formValidator({		
			validatorgroup:this.id,
        onshow: "请输入单元数!", onfocus: "请输入单元数!", oncorrect: " "
        }).functionValidator({ //数字验证
            fun:function(val,elem) { return val.isNum();}, onerror: "请输入数字"
        });
	},
	createForm:function(){
		return Chenxin.UI.FormCommon.communityBuildingInfoForm('communitybuilding');
	},
	initEvent:function(){			
		var _this=this;
		$('#communitybuilding_Save').live('click',function(){
			_this.save();
			return false;
		});
		$('#communitybuilding_Cancle').live('click',function(){
			_this.hide();
			return false;
		});	
	},
	createComboList:function(){
		
		var nameType=new Chenxin.UI.Combo({
				containerId: '#BuildingNameType',
				name:'BuildingNameType',
				addPleaseSelected:false,
				items:comboCommon.buildingNameType,
				required:true			
			});
		var unitType=new Chenxin.UI.Combo({
				containerId: '#UnitType',
				name:'UnitType',
				items:comboCommon.buildingUnitType
			});
	
		return [nameType,unitType];
	},
	initComponent:function(){
		Chenxin.UI.CommunityBuildingInfoForm.superclass.initComponent.call(this);
	}
});


/**
*查询面板
*/
Chenxin.UI.CommunityBuildingSearch=Chenxin.extend(Chenxin.UI.SearchCommon,{
	id:'building_search',
	initEvent:function(){
		var _this=this;
		$('#btn_search').live('click',function(){
			_this.scope.reloadGrid();
		});
	},
	createComboList:function(){	
		var rule=new Chenxin.UI.Combo({
			containerId: '#community_roomrule',
			name:'roomrule',
			tbxWidth:170,
			colNum:1,
			colWidth:210,
			items:[{
					val:'^[0-9]{3,4}$',
					txt:'楼层(1-2位)+房间号(数字2位)'
				},{
					val:'^[0-9]{1,2}[a-zA-Z]{1}$',
					txt:'楼层(1-2位)+房间号(字母1位)'
				},{
					val:'^[0-9]{1,2}([a-zA-Z]{1}|[0-9]{2})$',
					txt:'楼层(1-2位)+房间号(字母1位/数字2位)'
				},{
					val:'^[A-Z0-9]*$',
					txt:'大写字母,数字任意组合'
				},{
					val:'*',
					txt:'独(全)幢(无房间号)'
				},{
					val:'.',
					txt:'任意房间号'
				}]
		});
		var status=new Chenxin.UI.Combo({
			containerId: '#community_SearchStatus',
			name:'status',
			tbxWidth:70,
			items:[{txt:'未删除',val:'1,-10'},{txt:'正常',val:'1'},{txt:'已冻结',val:'-10'},{txt:'已删除',val:'-1'}]
		});
		return [rule,status];
	},
	initComponent:function(){
		Chenxin.UI.CommunityBuildingSearch.superclass.initComponent.call(this);
	}
});



/**
*@description 楼栋DIV
*@method 提交按钮的处理方法
*/
Chenxin.UI.FormCommon.communityBuildingInfoForm=function(comboIdPrefix){
        return '<table class="tbl_f"><tr><th><span class="isfilled">*</span>楼栋名称：</th><td><input type="hidden" name="BuildingId"/><input type="text" style="width:120px;" value="" class="inputbox_n fl" id="txtBuildingName" name="BuildingName"/><div id="txtBuildingNameTip" style="float:left; margin-left: 5px; width: 120px; height: 26px; line-height: 26px; text-align: left;"></div></td></tr><tr><th><span class="isfilled">*</span>类型：</th><td><div id="BuildingNameType" style="z-index:903; margin-right: 5px;" class="SelectWrapSingle validator_build"></div></td></tr><tr><th><span class="isfilled">*</span>总层数：</th><td><input type="text" style="width:120px;" value="" class="inputbox_n fl" id="txtFloor" name="Floor" /><div style="float:left;height: 26px; line-height:26px;padding-left:6px;">层</div><div id="txtFloorTip" style="float:left; margin-left: 5px; width: 120px; height: 26px; line-height: 26px; text-align: left;">请填写层数</div></td></tr><tr><th><span class="isfilled">*</span>单元数：</th><td><input type="text" style="width: 120px;" value="" class="inputbox_n fl" id="txtUnitNum" name="UnitNum" /><div id="txtUnitNumTip" style="float:left; margin-left: 5px; width: 120px; height: 26px; line-height: 26px; text-align: left;"></div></td></tr><tr><th>单元类型：</th><td> <div id="UnitType" style="z-index:902; margin-right: 5px;" class="SelectWrapSingle validator_build"></div></td></tr><tr><th>楼栋规则：</th><td><input type="radio" name="RoomRule" title="楼层(1-2位)+房间号(数字2位) [例：601，1804]" value="^[0-9]{3,4}$" checked="checked" class="lineh15"/>楼层(1-2位)+房间号(数字2位) [例：601，1804]<br /><input type="radio" name="RoomRule" title="楼层(1-2位)+房间号(字母1位) [例：6A，18D]" value="^[0-9]{1,2}[a-zA-Z]{1}$"  class="lineh15"/>楼层(1-2位)+房间号(字母1位) [例：6A，18D]<br /><input type="radio" name="RoomRule" title="楼层(1-2位)+房间号(字母1位/数字2位) [例：601，18D]" value="^[0-9]{1,2}([a-zA-Z]{1}|[0-9]{2})$"  class="lineh15"/>楼层(1-2位)+房间号(字母1位/数字2位) [例：601，18D]<br /><input type="radio" name="RoomRule" title="大写字母,数字任意组合 [例：601，18D]" value="^[A-Z0-9]*$"  class="lineh15"/>大写字母,数字任意组合 [例：601，18D]<br /><input type="radio" name="RoomRule" title="独(全)幢(无房间号)" value="*"  class="lineh15"/>独(全)幢(无房间号)<br /><input type="radio" name="RoomRule" title="任意房间号" value="."  class="lineh15"/>任意房间号<br /><input type="hidden" name="CommunityId"/><input type="hidden" name="Status"/></td></tr></table>'
};

/**
*@description 楼栋DIV
*@method 提交按钮的处理方法
*/
Chenxin.UI.FormCommon.communityBuildingInfoBulkForm=function(comboIdPrefix) {
        return '<table class="tbl_f"><tr><th><span class="isfilled">*</span>楼栋名称：</th><td><input type="text" class="inputbox_n" name="BuildingName" style="width:120px" value="*"/><span class="isfilled">通配符将替换"(*)"</span></td></tr><tr><th>步长：</th><td><input type="text" class="inputbox_n" name="step" value="1"  style="width:120px" /></td></tr><tr><th></th><td>楼栋名称通配符设置：</td></tr><tr><th></th><td><input type="radio" name="setting" class="lineh15" checked="checked" value="number"/> 从 <input type="text" class="inputbox_n"  style="width:30px" name="MinNumber" value="1" /> 到 <input type="text" class="inputbox_n" style="width:30px" name="MaxNumber" value="1" /> 长度 <input type="text" class="inputbox_n" name="LengthNumber" value="2"  style="width:30px"/>(1-5) </td></tr><tr><th></th><td><input type="radio" name="setting" class="lineh15" value="char"/> 从 <input type="text" class="inputbox_n"  style="width:30px" name="MinChar" value="A" /> 到 <input type="text" class="inputbox_n" style="width:30px" name="MaxChar" value="Z" /></td></tr><tr><th><span class="isfilled">*</span>类型：</th><td><div id="' + comboIdPrefix + '_BuildingNameType" style="z-index:903; margin-right: 5px;" class="SelectWrapSingle validator_build"></div></td></tr><tr><th><span class="isfilled">*</span>总层数：</th><td><input type="text" style="width:120px;" value="" class="inputbox_n fl" id="' + comboIdPrefix + '_Floor" name="Floor" /><div style="float:left;height: 26px; line-height:26px;padding-left:6px;">层</div><div id="' + comboIdPrefix + '_FloorTip" style="float:left; margin-left: 5px; width: 120px; height: 26px; line-height: 26px; text-align: left;">请填写层数</div></td></tr><tr><th><span class="isfilled">*</span>单元数：</th><td><input type="text" style="width: 120px;" value="" class="inputbox_n fl" id="' + comboIdPrefix + '_UnitNum" name="UnitNum" /><div id="' + comboIdPrefix + '_UnitNumTip" style="float:left; margin-left: 5px; width: 120px; height: 26px; line-height: 26px; text-align: left;"></div></td></tr><tr><th>单元类型：</th><td> <div id="' + comboIdPrefix + '_UnitType" style="z-index:902; margin-right: 5px;" class="SelectWrapSingle validator_build"></div></td></tr><tr><th>楼栋规则：</th><td><input type="radio" name="RoomRule" title="楼层(1-2位)+房间号(数字2位) [例：601，1804]" value="^[0-9]{3,4}$" checked="checked" class="lineh15"/>楼层(1-2位)+房间号(数字2位) [例：601，1804]<br /><input type="radio" name="RoomRule" title="楼层(1-2位)+房间号(字母1位) [例：6A，18D]" value="^[0-9]{1,2}[a-zA-Z]{1}$"  class="lineh15"/>楼层(1-2位)+房间号(字母1位) [例：6A，18D]<br /><input type="radio" name="RoomRule" title="楼层(1-2位)+房间号(字母1位/数字2位) [例：601，18D]" value="^[0-9]{1,2}([a-zA-Z]{1}|[0-9]{2})$"  class="lineh15"/>楼层(1-2位)+房间号(字母1位/数字2位) [例：601，18D]<br /><input type="radio" name="RoomRule" title="大写字母,数字任意组合 [例：601，18D]" value="^[A-Z0-9]*$"  class="lineh15"/>大写字母,数字任意组合 [例：601，18D]<br /><input type="radio" name="RoomRule" title="独(全)幢(无房间号)" value="*"  class="lineh15"/>独(全)幢(无房间号)<br /><input type="radio" name="RoomRule" title="任意房间号" value="."  class="lineh15"/>任意房间号<br /></td></tr><tr><td colspan="2" align="right"><div style="float:right; margin-right:15px;"><input type="hidden" name="CommunityId"/><a id="' + comboIdPrefix + '_Save" class="btn_n fl" href="#"><span>确定</span></a><a id="' + comboIdPrefix + '_Cancle"  class="btn_n fl" href="#"><span>取消</span></a></div></td></tr></table>'
};




















